<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

/**
 * App\Models\CouponRule
 *
 * @property int $rule_id 主键自增无意义
 * @property string $title 规则名称
 * @property string $brief 规则描述
 * @property string $reduce_type 优惠类型 subtarct：满减  subtract：免邮  discount:折扣
 * @property string|null $conditions 商品、品牌、分类 满多少等条件序列化
 * @property string|null $actions 优惠方案序列化
 * @property int $p_order 排序
 * @property int $disabled 是否删除 0-否 1-是
 * @property \Carbon\Carbon|null $created_at
 * @property \Carbon\Carbon|null $updated_at
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\CouponRule whereActions($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\CouponRule whereBrief($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\CouponRule whereConditions($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\CouponRule whereCreatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\CouponRule whereDisabled($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\CouponRule wherePOrder($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\CouponRule whereReduceType($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\CouponRule whereRuleId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\CouponRule whereTitle($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\CouponRule whereUpdatedAt($value)
 * @mixin \Eloquent
 */
class CouponRule extends Model
{
    protected $table = 'coupon_rule';
    protected $primaryKey = 'rule_id';
    protected $guarded = ['out_other','type','rule_id'];

    public $error;

    //规则类型映射
    public $rule_type = [
        'subtract' => '满减',
        'freeship' => '免邮',
        'discount' => '折扣'
    ];

    /**规则的添加或者修改
     * @param $attributes
     * @return bool
     */
    public function save_rule($attributes)
    {
        if (!$attributes) {
            return true;
        }
        $rule_id = empty($attributes['rule_id']) ? 0 : $attributes['rule_id'];
        $condition_attributes = $attributes['Condition'];
        $action_attributes =  $attributes['Action'];
        $attributes = $attributes['Rule'];
        $attributes['rule_id'] = $rule_id;
        //规则条件
        if ($condition_attributes['all'] == 1) {
            $condition_arr = [
                'all'=>1,
                'min_amount'=>$condition_attributes['min_amount'],
                'cat'=>[],
                'brand'=>[],
                'goods'=>[],
            ];
        } else {
            $condition_arr = [
                'all'=>0,
                'min_amount'=>$condition_attributes['min_amount'],
                'cat'=>$condition_attributes['cat']?explode(',', $condition_attributes['cat']):[],
                'brand'=>$condition_attributes['brand'],
                'goods'=>$condition_attributes['goods'],
            ];
        }

        //优惠方案
        $action_arr = [];
        if ($attributes['reduce_type'] == 'subtract') {
            $action_arr = [
                'order_amount'=>$action_attributes['order_amount'],
                'goods_amount'=>$action_attributes['goods_amount'],
            ];
        } elseif ($attributes['reduce_type'] == 'freeship') {
            $action_arr = [
                'ship_amount'=>$action_attributes['ship_amount'],
            ];
        } elseif ($attributes['reduce_type'] == 'discount') {
            $action_arr = [
                'discount'=>$action_attributes['discount'],
            ];
        }
        $attributes['conditions'] = serialize($condition_arr);
        $attributes['actions'] = serialize($action_arr);

        unset($attributes['out_other'],$attributes['type']);
        if(!empty($attributes['rule_id'])){
            $res = self::query()->where('rule_id',$attributes['rule_id'])->update($attributes);
        }else{
            $res = self::create($attributes);
        }
        if(!$res){
            $this->error = '保存优惠券规则失败';
            return false;
        }

        return true;
    }
}
